package xiaodou.core.WeiboData
{
	import flash.events.ErrorEvent;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.utils.Dictionary;
	
	import spark.formatters.DateTimeFormatter;
	
	import xiaodou.core.ObjectUtil;
	import xiaodou.core.WeiboData.enums.GenderEnum;
	import xiaodou.core.WeiboData.events.WeiboDataEvent;
	import xiaodou.core.WeiboData.events.WeiboEvent;

	public class WeiboFansInfo extends EventDispatcher
	{
		protected var  _AryActiveFollowers:Array = new Array();
		protected var  _aryFollowers:Array = new Array();
		public static var  ProvinceFansCountDic:Dictionary = new Dictionary();//粉丝地域统计
		public static var  FansGenderCountDic:Dictionary = new Dictionary();  //粉丝性别统计
		public static var  FansVerifiedCountDic:Dictionary = new Dictionary();//粉丝加V
		public static var  FansAccountTimeCountDic:Dictionary = new Dictionary();//粉丝注册时长
		public static var  FansTagCountDic:Dictionary = new Dictionary()//粉丝标签
		public static var  FansFollowersCountDic:Dictionary = new Dictionary()//粉丝的粉丝
		
		public static const millisecondsPerMinute:int = 1000 * 60;
		public static const millisecondsPerHour:int = 1000 * 60 * 60;
		public static const millisecondsPerDay:int = 1000 * 60 * 60 * 24;
		
		public static var _aryFansCreateWeiboDays:Array = new Array();//这里是设置粉丝注册微博账号时长的分析区间，如1个月，2-3个月，3-6个月，半年-1年等，转化成为天，便于计算统计
		public static var _aryFollowersCount:Array = new Array();
		protected var _aryFansCreateWeiboDate:Array = new Array();//根据_aryFansCreateWeiboTime生成的具体日期，用于得出具体的日期时间，以做粉丝注册微博账号时长
	
		
		public function WeiboFansInfo()
		{
			if(_aryFansCreateWeiboDays.length == 0)
			{
				_aryFansCreateWeiboDays.push(0,30,90,180,365,545,730,910);//1个月，2-3个月，3-6个月，半年-1年等，转化成为天，便于计算统计
				
				var Today:Date = new Date();
				for each(var Days:int in _aryFansCreateWeiboDays)//计算出具体时间，存到_aryFansCreateWeiboDate
				{
					var newDate:Date = new Date(Today.getTime() - Days*millisecondsPerDay);
					_aryFansCreateWeiboDate.push(newDate);
					
					FansAccountTimeCountDic[Days] = 0;
				}
			}
			_aryFollowersCount.push(9,49,99,199,299,399,499,999,1999,4999,9999);
			FansGenderCountDic["m"] = 0;
			FansGenderCountDic["f"] = 0;
			FansGenderCountDic["n"] = 0;
			
			FansVerifiedCountDic["Verified"] = 0;
			FansVerifiedCountDic["UnVerified"] = 0;
		}
		
		public function RemoveAllData():void
		{
			_AryActiveFollowers.splice(0);
			_aryFollowers.splice(0);
			
			FansGenderCountDic["m"] = 0;
			FansGenderCountDic["f"] = 0;
			FansGenderCountDic["n"] = 0;
			
			FansVerifiedCountDic["Verified"] = 0;
			FansVerifiedCountDic["UnVerified"] = 0;
			
			
			ProvinceFansCountDic = new Dictionary();
			FansTagCountDic      = new Dictionary();
			FansFollowersCountDic = new Dictionary();
			
			for each(var Days:int in _aryFansCreateWeiboDays)//
			{
				FansAccountTimeCountDic[Days] = 0;
			}
			
		}
		
		/**
		 *活跃粉丝 
		 */
		public function set AryActiveFollowers(value:Array):void
		{
			_AryActiveFollowers = value;
			var Evt:WeiboEvent = new WeiboEvent(WeiboDataEvent.ACTIVE_FOLLOWER_SUCCESS);
			dispatchEvent(Evt);
		}
		
		public function get AryActiveFollowers():Array
		{
			return _AryActiveFollowers;
		}

		public function PushActiveFollower(obj:Object):void
		{
			AryActiveFollowers.push(obj);
		}
		
		public function PushFollower(obj:Object):void
		{
			var fan:WeiboFan = new WeiboFan();
			ObjectUtil.fill(obj, fan);
			
			var index:int = getWeiboFanIndex(fan.id);
			if(index != -1)
				_aryFollowers.splice(index, 1);
			_aryFollowers.push(fan);
			var Evt:WeiboEvent = new WeiboEvent(WeiboDataEvent.GET_FANS_INFO_SUCCESS);
			Evt.message = fan.id;
			dispatchEvent(Evt);
		}
		
		public function PushFollowersBatch(value:Array):void
		{
			for each(var fan:Object in value)
			{
				//分析省份
				if(ProvinceFansCountDic[fan["province"]] == null)
				{
					var ProvinceFans:int = 1;
					ProvinceFansCountDic[fan["province"]]  = ProvinceFans;	
				}
				else
				{
					ProvinceFansCountDic[fan["province"]]++;
				}
				
				//分析性别
				FansGenderCountDic[fan["gender"]]++;

				
				//加V
				if(fan["verified"])
				{
					FansVerifiedCountDic["Verified"]++;
				}
				else
				{
					FansVerifiedCountDic["UnVerified"]++;
				}
				
				//粉丝数
				for(var i:int = 0; i < _aryFollowersCount.length; ++i)
				{
					if(fan["followers_count"] < _aryFollowersCount[i])
					{
						if(FansFollowersCountDic[_aryFollowersCount[i]] != null)
						{
							FansFollowersCountDic[_aryFollowersCount[i]]++;
						}
						else
						{
							FansFollowersCountDic[_aryFollowersCount[i]] = 1;
						}
						break;
					}
					
					if(i == _aryFollowersCount.length-1 && fan["followers_count"] > _aryFollowersCount[i])
					{
						if(FansFollowersCountDic[_aryFollowersCount[i]] != null)
						{
							FansFollowersCountDic[_aryFollowersCount[i]]++;
						}
						else
						{
							FansFollowersCountDic[_aryFollowersCount[i]] = 1;
						}
					}
				}
			
				
				//注册微博账号时长
				var createdDate:Date = new Date(Date.parse(fan["created_at"]));
				for(var j:int = _aryFansCreateWeiboDate.length-1; j >= 0; --j)
				{
					
					if(createdDate < _aryFansCreateWeiboDate[j])
					{
						FansAccountTimeCountDic[_aryFansCreateWeiboDays[j]]++;
						break;
					}
				}
			}
			
			var Evt:WeiboEvent = new WeiboEvent(WeiboDataEvent.FANS_ANALYTICS_SUCCESS);
			dispatchEvent(Evt);
			
		}
		
		public function PushFansTag(tag:Object):void
		{
			for(var key:* in tag)
			{
				if(key != "weight")
				{
					if(FansTagCountDic[tag[key].toString()]== null)
					{
						var count:int = 1;
						FansTagCountDic[tag[key].toString()] = count;
					}
					else
					{
						FansTagCountDic[tag[key].toString()] ++;
					}
					
					break;
				}
			}
		}
		
		/**
		 *获得粉丝的标签信息 
		 * @return  
		 * 
		 */		
		public function GetFansTagCount():Array
		{
			var ary:Array = new Array();
			for(var key:* in FansTagCountDic)
			{
				var obj:Object = {
					TagName:key,
					TagCount: FansTagCountDic[key]};
				
				trace("key:" + obj.TagName + "  count:" +  obj.TagCount.toString());
				ary.push(obj);
			}
			
			ary.sortOn("TagCount", Array.NUMERIC|Array.DESCENDING);
			return ary;
		}
		
		public function FansTagCountIsReady():void
		{
			var Evt:WeiboEvent = new WeiboEvent(WeiboDataEvent.FANS_TAG_SUCCESS);
			dispatchEvent(Evt);
		}
		
		private function getWeiboFanIndex(id:String):int
		{
			for (var i:int = 0; i < _aryFollowers.length; i++) 
			{
				var obj:WeiboFan = _aryFollowers[i];
				if(obj.id == id)
					return i;
			}
			return -1;
			
		}
		
		public function GetFollwerbyID(id:String):WeiboFan
		{
			for each (var obj:WeiboFan in _aryFollowers) 
			{
				if(obj.id == id)
				{
					return obj;
				}
			}
			
			return null;
		}
		
		/**
		 * 获得粉丝省分布信息 
		 * @return [{"FansCount":2,"ProvinceName":"江苏"},{"FansCount":1,"ProvinceName":"香港"},{"FansCount":1,"ProvinceName":"重庆"},....]
		 * 
		 */		
		public function GetFansProvinceInfo():Array
		{
			var arr:Array = new Array();
			for (var key:* in ProvinceFansCountDic)
			{
				var Obj:Object = {
					ProvinceName: ObjectUtil.GetProvinceNameByID(key),
					FansCount:ProvinceFansCountDic[key]
				};
				
				arr.push(Obj);
			}
			
			return arr;
		}
		
		/**
		 * 获得粉丝性别信息 
		 * @return [{"FansCount":17,"Gender":"m"},{"FansCount":5,"Gender":"f"},{"FansCount":0,"Gender":"n"}] m代表男，f代表女 ,n 代表未知
		 * 
		 */		
		public function GetFansGenderInfo():Array
		{
			var arr:Array = new Array();
			for (var key:* in FansGenderCountDic)
			{
				var Obj:Object = {
					Gender: GenderEnum.toString(key),
					FansCount:FansGenderCountDic[key]
				};
				
				arr.push(Obj);
			}
			
			return arr;
		}
		
		/**
		 * 获得粉丝认证信息 
		 * @return [{"FansCount":21,"Verified":false},{"FansCount":1,"Verified":true}]
		 * 
		 */		
		public function GetFansVerifiedInfo():Array
		{
			var arr:Array = new Array();
			for (var key:* in FansVerifiedCountDic)
			{
				var Obj:Object = {
					Verified: key.toString() == "Verified",
					FansCount:FansVerifiedCountDic[key]
				};
				
				arr.push(Obj);
			}
			
			return arr;
		}
		
		/**
		 * 获得粉丝的粉丝数量信息 
		 * @return 返回一个包含粉丝数量信息的数组，{"FollowersCount":9,"FansCount":20}...
		 * 
		 */		
		
		public function GetFansFollowersCountInfo():Array
		{
			var arr:Array = new Array();
			for (var key:* in FansFollowersCountDic )
			{
				var Obj:Object = {
						FollowersCount:key,
						FansCount:FansFollowersCountDic[key]
				};
				arr.push(Obj);
			}
			
			return arr;
		}
		/**
		 * 获得粉丝账号注册时间信息 
		 * Days代表注册的时长 0代表今天 30代表一个月内 180代表半年内（6个月）
		 * @return
		 * 
		 */		
		public function GetFansAccountCreateTimeInfo():Array
		{
			var arr:Array = new Array();
			for (var key:* in FansAccountTimeCountDic)
			{
				var Obj:Object = {
					Days: key,
					FansCount:FansAccountTimeCountDic[key]
				};
				
				arr.push(Obj);
			}
			
			return arr;
		}
	}
}